package org.paul.agoto.ui;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import org.paul.agoto.R;
import org.paul.agoto.bean.LoginBean;
import org.paul.agoto.manager.ApiManager;
import org.paul.library.base.BaseAct;
import org.paul.library.manager.ThreadManager;

import java.util.HashMap;
import java.util.Map;

public class ApiTesterAct extends BaseAct implements AdapterView.OnItemClickListener {
    private static final String EMPTY = "";
    TextView textView;
    final int LOGIN_SUCCESS = 0x11;
    final int LOGIN_FAILED = 0x12;

    @Override
    protected void initUi() {
        ListView listView = $(R.id.listView);
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, android.R.id.text1, getResources().getStringArray(R.array.apis));
        listView.setAdapter(arrayAdapter);
        listView.setOnItemClickListener(this);
        textView = $(R.id.textView);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        textView.setText(EMPTY);
        switch (position) {
            case 0:
                login();
                break;
            case 1:
                startAct(BasicActivity.class);
                break;
            case 2:
                startAct(BottomNavigationActivity.class);
                break;
            case 3:
                startAct(FullscreenActivity.class);
                break;
            case 4:
                startAct(ItemListActivity.class);
                break;
            case 5:
                startAct(LoginActivity.class);
                break;
            case 6:
                startAct(NavigationDrawerActivity.class);
                break;
            case 7:
                startAct(ScrollingActivity.class);
                break;
            case 8:
                startAct(SettingsActivity.class);
                break;
            case 9:
                startAct(TabbedActivity.class);
                break;
        }
    }

    @SuppressLint("HandlerLeak")
    Handler handler = new Handler(callback) {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case LOGIN_SUCCESS:
                    textView.setText(((LoginBean) msg.obj).toString());
                    break;
                case LOGIN_FAILED:
                    break;
            }
        }
    };

    @Override
    public Handler getHandler() {
        return handler;
    }

    private void login() {
        showProgress();
        ApiManager.getInstance().login(loginParams(), loginHandleRet);
    }

    private ThreadManager.HandleRet<LoginBean> loginHandleRet =
            (loginBean)->{
                handle(loginBean, LOGIN_SUCCESS, LOGIN_FAILED);
            };

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    @Override
    protected void onStop() {
        super.onStop();
        loginHandleRet.cancel();
    }

    private Map<String, Object> loginParams() {
        Map<String, Object> ret = new HashMap<>();
        ret.put("username", "tmpa@itm.com");
        ret.put("password", "Deep1234");
        ret.put("loginType", "EM");
        return ret;
    }
}
